import websocket
import ssl
from google.protobuf.json_format import MessageToDict
import kuaishou_pb2
from browse_token import get_live_info
import time

try:
    import thread
except ImportError:
    import _thread as thread


def on_message(ws, websocket):

    Message = kuaishou_pb2.SocketMessage()
    Message.ParseFromString(websocket)
    if Message.payloadType == 310:
        SCWebFeedPUsh = kuaishou_pb2.SCWebFeedPush()
        SCWebFeedPUsh.ParseFromString(Message.payload)
        obj = MessageToDict(SCWebFeedPUsh, preserving_proto_field_name=True)
        if obj.get('commentFeeds', ''):
            msg_list = obj.get('commentFeeds', '')
            for i in msg_list:
                userName = i['user']['userName']
                pid = i['user']['principalId']
                content = i['content']
                print("%s  -->  %s  -->  %s" % (userName, pid, content))
        if obj.get('giftFeeds', ''):
            msg_list = obj.get('giftFeeds', '')
            for i in msg_list:
                userName = i['user']['userName']
                pid = i['user']['principalId']
                print("%s  -->  %s" % (userName, pid))
        if obj.get('likeFeeds', ''):
            msg_list = obj.get('likeFeeds', '')
            for i in msg_list:
                userName = i['user']['userName']
                pid = i['user']['principalId']
                print("%s -->  %s" % (userName, pid))


def on_error(ws, error):
    print(f"Error: {error}")


def on_close(ws, close_status_code, close_msg):
    ws.run_forever(sslopt={"cert_reqs": ssl.CERT_NONE})


def hex_(n):
    res = []
    while n > 128:
        res.append(int((n & 127) | 128))
        n = n >> 7
    res.append(int(n))
    return res


def on_open(ws, token, liveStreamId):
    part1 = [0x08, 0xC8, 0x01, 0x1A, 0xC9, 0x01, 0x0A, 0x98, 0x01]  # 不变的头
    part2 = [ord(c) for c in token]
    part3 = [0x12, 0x0B]
    part4 = [ord(c) for c in liveStreamId]
    part5 = [ord(c) for c in 'B']
    part6 = [0x0b]
    part7 = [ord(c) for c in 'KUAISHOU_H5J']
    part8 = [0x12]
    part9 = [ord(c) for c in 'OUTSIDE_ANDROID_H5']
    d = part1 + part2 + part3 + part4 + part5 + part6 + part7 + part8 + part9
    d = bytes(d)
    ws.send(d, websocket.ABNF.OPCODE_BINARY)

    def run():
        while True:
            time.sleep(20)
            # 发送心跳-当前时间戳-毫秒
            head = [0x08, 0x01, 0x1A, 0x07, 0x08]
            timestamp = int(time.time() * 1000)
            time_arr = hex_(timestamp)
            heartbeat = bytes(head + time_arr)
            ws.send(heartbeat, websocket.ABNF.OPCODE_BINARY)

    thread.start_new_thread(run, ())


if __name__ == "__main__":

    room_number='3xkvxwkfzfzvkac' # 设置直播间id
    liveStreamId, kwaiId, token, webSocketAddresses = get_live_info(room_number)
    websocket.enableTrace(True)
    ws = websocket.WebSocketApp(webSocketAddresses,
                                header=[
                                    "Pragma: no-cache",
                                    "Origin: https://livev.m.chenzhongtech.com",
                                    "Accept-Language: zh-CN,zh;q=0.9,ja;q=0.8,sq;q=0.7,he;q=0.6",
                                    "Sec-WebSocket-Key: B/qwGkSv37yD2+r1/5bGSw==",
                                    "User-Agent: Mozilla/5.0 (iPhone; CPU iPhone OS 16_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.6 Mobile/15E148 Safari/604.1",
                                    "Upgrade: websocket",
                                    "Cache-Control: no-cache",
                                    "Connection: Upgrade",
                                    "Sec-WebSocket-Version: 13",
                                    "Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits"
                                ],
                                on_message=on_message,
                                on_error=on_error,
                                on_close=on_close)

    ws.on_open = lambda ws: on_open(ws, token, liveStreamId)
    ws.run_forever(sslopt={"cert_reqs": ssl.CERT_NONE})

    # message = '08d40210011a99230a84010a7f0a08487a3230303030301207e79588e887a32e1a6a68747470733a2f2f70342d70726f2e612e7978696d67732e636f6d2f75686561642f41422f323032322f30342f33302f30302f424d6a41794d6a41304d7a41774d4449774e4464664e546b344f4451334d446734587a4666614751794e445a664f4441795f732e6a70672a01320a650a600a0f33786e707571616968726a703234651221e5928ce58da1e58da1e8a5bfe4b880e8b5b7e79c8be4bab2e783ade5a4a9e5a0821a2a687474703a2f2f70352e612e7978696d67732e636f6d2f73312f692f6465662f686561645f662e706e672a01320a93010a8d010a0f3378616e6839716d35687373787073120e2ae29886e6b885e9a38ee298862a1a6a68747470733a2f2f70322d70726f2e612e7978696d67732e636f6d2f75686561642f41422f323031382f30322f32352f30312f424d6a41784f4441794d6a55774d5455344d6a5a664f4459344e7a4d794e544578587a4a666147517a4d444e664d7a513d5f732e6a70672a01310aa2010a9c010a0f3378773635623639783432743663341219e5bfabe6898be794a8e688b7313731333935303735383238351a6e68747470733a2f2f70322d70726f2e612e7978696d67732e636f6d2f75686561642f41422f323032342f30342f32362f30322f424d6a41794e4441304d6a59774d6a457a4e4446664e4445774f4445774d444d794d4638784d31396f5a4463784d3138784e6a593d5f732e6a70672a01310a5d0a580a0f33786a676678676468616e786479651219e5bfabe6898be794a8e688b7313731333935303734353733341a2a687474703a2f2f70342e612e7978696d67732e636f6d2f73312f692f6465662f686561645f752e706e672a01310a98010a92010a0f33783233716461366470396d746365120fe89299e5a89ce4b8bde88e8ee3819b1a6e68747470733a2f2f70322d70726f2e612e7978696d67732e636f6d2f75686561642f41422f323032342f30342f32362f30322f424d6a41794e4441304d6a59774d6a457a4e4442664e4445774f4441354f5463314d4638784d31396f5a4467304e6c387a4d7a673d5f732e6a70672a01310a92010a8c010a0f3378613364367262793438706662341209e79c9fe4baa6e581871a6e68747470733a2f2f70322d70726f2e612e7978696d67732e636f6d2f75686561642f41422f323032342f30342f32362f30322f424d6a41794e4441304d6a59774d6a457a4d7a6c664e4445774f4441354f544d784d5638784d31396f5a44497a4e3138314f44493d5f732e6a70672a01310a96010a90010a0f337866373369716a7a676b796d6275120d2de5a4b2e4babce58589e9949f1a6e68747470733a2f2f70322d70726f2e612e7978696d67732e636f6d2f75686561642f41422f323032342f30342f32362f30322f424d6a41794e4441304d6a59774d6a457a4d7a68664e4445774f4441354f5441344e3138784d31396f5a44457a4f4638784e544d3d5f732e6a70672a01310aa2010a9c010a0f33786e6a6162626b6b773461646b711219e5bfabe6898be794a8e688b7313731333935303639373839361a6e68747470733a2f2f70342d70726f2e612e7978696d67732e636f6d2f75686561642f41422f323032342f30342f32362f30322f424d6a41794e4441304d6a59774d6a457a4d7a68664e4445774f4441354f4467334d4638784d31396f5a444d354e6c38334d546b3d5f732e6a70672a01310aa2010a9c010a0f33786a34387264676a367a6e356e611219e5bfabe6898be794a8e688b7313731333935303638343637301a6e68747470733a2f2f70342d70726f2e612e7978696d67732e636f6d2f75686561642f41422f323032342f30342f32362f30322f424d6a41794e4441304d6a59774d6a457a4d7a64664e4445774f4441354f4455334e6c38784d31396f5a4467774d46387a4f44633d5f732e6a70672a01310a95010a8f010a0f337862396465626577683772777939120ce4b880e7ac91e582bee59f8e1a6e68747470733a2f2f70342d70726f2e612e7978696d67732e636f6d2f75686561642f41422f323032342f30342f32362f30322f424d6a41794e4441304d6a59774d6a457a4d7a5a664e4445774f4441354f4449344d6c38784d31396f5a446b794d4638314f54593d5f732e6a70672a01310a98010a92010a0f33787a6e7a756870766d7569737532120fe29681e4bb85e5ad98e4be9de99da01a6e68747470733a2f2f70322d70726f2e612e7978696d67732e636f6d2f75686561642f41422f323032342f30342f32362f30322f424d6a41794e4441304d6a59774d6a457a4d7a56664e4445774f4441354f44417a4d3138784d31396f5a4467344e3138324e7a493d5f732e6a70672a01310a98010a92010a0f3378376e7137683273707639343539120fe5b091e5b9b4e58c85e99d92e89b991a6e68747470733a2f2f70322d70726f2e612e7978696d67732e636f6d2f75686561642f41422f323032342f30342f32362f30322f424d6a41794e4441304d6a59774d6a457a4d7a56664e4445774f4441354e7a63324e6c38784d31396f5a444d304d5638304f446b3d5f732e6a70672a01310a95010a8f010a0f33787a373965377367643768716336120ce5a4aae999bde4b88de687821a6e68747470733a2f2f70322d70726f2e612e7978696d67732e636f6d2f75686561642f41422f323032342f30342f32362f30322f424d6a41794e4441304d6a59774d6a457a4d7a52664e4445774f4441354e7a55784f4638784d31396f5a444d33587a59304e513d3d5f732e6a70672a01310a95010a8f010a0f3378746d6267626133327532367571120ce68c91e781afe59b9ee69c9b1a6e68747470733a2f2f70322d70726f2e612e7978696d67732e636f6d2f75686561642f41422f323032342f30342f32362f30322f424d6a41794e4441304d6a59774d6a457a4d7a4e664e4445774f4441354e7a4d774d3138784d31396f5a4463304e5638314d6a413d5f732e6a70672a01310a92010a8c010a0f337833796164706e683438327371631209e89192e9a29ce8888a1a6e68747470733a2f2f70322d70726f2e612e7978696d67732e636f6d2f75686561642f41422f323032342f30342f32362f30322f424d6a41794e4441304d6a59774d6a457a4d7a4a664e4445774f4441354e6a6b7a4e5638784d31396f5a4449354d6c38354d44633d5f732e6a70672a01310a95010a8f010a0f3378706b633439327936686176676b120ce297a3e6838ae889b3e284a11a6e68747470733a2f2f70322d70726f2e612e7978696d67732e636f6d2f75686561642f41422f323032342f30342f32362f30322f424d6a41794e4441304d6a59774d6a457a4d7a46664e4445774f4441354e6a677a4e4638784d31396f5a4451314e4638354e7a593d5f732e6a70672a01310a92010a8c010a0f33786473336e696d7064767137646d1209e6a182e88ab1e7949c1a6e68747470733a2f2f70352d70726f2e612e7978696d67732e636f6d2f75686561642f41422f323032342f30342f32362f30322f424d6a41794e4441304d6a59774d6a457a4d7a42664e4445774f4441354e6a677a4d3138784d31396f5a4449774d3138324e773d3d5f732e6a70672a01310aa2010a9c010a0f3378793465687675677667707078391219e5bfabe6898be794a8e688b7313731333935303630393731351a6e68747470733a2f2f70322d70726f2e612e7978696d67732e636f6d2f75686561642f41422f323032342f30342f32362f30322f424d6a41794e4441304d6a59774d6a457a4d7a42664e4445774f4441354e6a67784d6c38784d31396f5a4459774f4638784f444d3d5f732e6a70672a01310a95010a8f010a0f33787775796e6870737578696d6e61120ce788b1e4bda0e788b1e588b01a6e68747470733a2f2f70342d70726f2e612e7978696d67732e636f6d2f75686561642f41422f323032342f30342f32362f30322f424d6a41794e4441304d6a59774d6a457a4d6a6c664e4445774f4441354e6a63344d4638784d31396f5a4463314d6c38324f446b3d5f732e6a70672a01310a95010a8f010a0f33786d7077796665787a6e38623436120ce69a96e586ace4be9de697a71a6e68747470733a2f2f70322d70726f2e612e7978696d67732e636f6d2f75686561642f41422f323032342f30342f32362f30322f424d6a41794e4441304d6a59774d6a457a4d6a68664e4445774f4441354e6a63324e5638784d31396f5a444d774e5638314d6a4d3d5f732e6a70672a01310aa2010a9c010a0f3378613333693365367436746267691219e5bfabe6898be794a8e688b7313731333935303630363936391a6e68747470733a2f2f70352d70726f2e612e7978696d67732e636f6d2f75686561642f41422f323032342f30342f32362f30322f424d6a41794e4441304d6a59774d6a457a4d6a64664e4445774f4441354e6a63324d5638784d31396f5a4449784d4638334e54513d5f732e6a70672a01310a95010a8f010a0f3378336a666b663864617779767932120ce58fafe58d8ee7be8ee890a51a6e68747470733a2f2f70342d70726f2e612e7978696d67732e636f6d2f75686561642f41422f323032342f30342f32362f30322f424d6a41794e4441304d6a59774d6a457a4d6a64664e4445774f4441354e6a637a4d3138784d31396f5a4459774e5638344e44673d5f732e6a70672a01310a92010a8c010a0f337869686b76683271327072356e361209e7ac99e6ad8ce5889d1a6e68747470733a2f2f70322d70726f2e612e7978696d67732e636f6d2f75686561642f41422f323032342f30342f32362f30322f424d6a41794e4441304d6a59774d6a457a4d6a5a664e4445774f4441354e6a637a4d6c38784d31396f5a4451354e56387a4d7a493d5f732e6a70672a01310aa2010a9c010a0f3378356a7571656b6d7369327870361219e5bfabe6898be794a8e688b7313731333935303630353732301a6e68747470733a2f2f70322d70726f2e612e7978696d67732e636f6d2f75686561642f41422f323032342f30342f32362f30322f424d6a41794e4441304d6a59774d6a457a4d6a56664e4445774f4441354e6a63794e3138784d31396f5a446b304d3138784e7a553d5f732e6a70672a01310a95010a8f010a0f337869626d6374393238386436346d120ce4b88de695a2e5a684e8a8801a6e68747470733a2f2f70342d70726f2e612e7978696d67732e636f6d2f75686561642f41422f323032342f30342f32362f30322f424d6a41794e4441304d6a59774d6a457a4d6a52664e4445774f4441354e6a63784d4638784d31396f5a4455314e5638334e413d3d5f732e6a70672a01310a92010a8c010a0f337838383233646d3675666d6b61321209e8aaaae694bee6a3841a6e68747470733a2f2f70322d70726f2e612e7978696d67732e636f6d2f75686561642f41422f323032342f30342f32362f30322f424d6a41794e4441304d6a59774d6a457a4d6a4a664e4445774f4441354e6a59354d3138784d31396f5a4459344d6c387a4e7a593d5f732e6a70672a01310a95010a8f010a0f33786d6963686874767737346b7839120ce5b08fe7949fe6b4bbe296b31a6e68747470733a2f2f70352d70726f2e612e7978696d67732e636f6d2f75686561642f41422f323032342f30342f32362f30322f424d6a41794e4441304d6a59774d6a457a4d6a46664e4445774f4441354e6a59354d5638784d31396f5a4463354f4638314d6a673d5f732e6a70672a01310a8f010a89010a0f33787736623761616d7536647338751206e4bd99e6b8a91a6e68747470733a2f2f70342d70726f2e612e7978696d67732e636f6d2f75686561642f41422f323032342f30342f32362f30322f424d6a41794e4441304d6a59774d6a457a4d6a42664e4445774f4441354e6a59344f4638784d31396f5a4467774d5638334d6a6b3d5f732e6a70672a01310a98010a92010a0f33786a376873726a7a736a66713563120fe4ba8ee8b49fe88db7e4b88de8b5b71a6e68747470733a2f2f70352d70726f2e612e7978696d67732e636f6d2f75686561642f41422f323032342f30342f32362f30322f424d6a41794e4441304d6a59774d6a457a4d6a4e664e4445774f4441354e6a59334e5638784d31396f5a4459344f56387a4f54413d5f732e6a70672a013120adfdf6cf8032'
    #
    # d = bytes.fromhex(message)
    # print(d)
